package cn.thoughtworks.school.programCenter.controllers;

import cn.thoughtworks.school.programCenter.annotations.Auth;
import cn.thoughtworks.school.programCenter.entities.Tag;
import cn.thoughtworks.school.programCenter.entities.User;
import cn.thoughtworks.school.programCenter.entities.vo.StudentTag;
import cn.thoughtworks.school.programCenter.exceptions.BusinessException;
import cn.thoughtworks.school.programCenter.repositories.TagRepository;
import cn.thoughtworks.school.programCenter.services.UserTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/api/programs")
public class UserTagController {
    @Autowired
    private UserTagService userTagService;

    @GetMapping("{programId}/tags")
    public ResponseEntity getTagsByProgramId(@PathVariable Long programId) {
        List<StudentTag> tags = userTagService.findTagsByProgramId(programId);
        return ResponseEntity.ok(tags);
    }

    @PutMapping("tags/{tagId}")
    public ResponseEntity getTagsByProgramId(@PathVariable Long tagId, @RequestBody Tag tag) throws BusinessException {
        return new ResponseEntity(userTagService.updateTag(tagId, tag), HttpStatus.NO_CONTENT);
    }

    @PostMapping("tags")
    public ResponseEntity addTag(@RequestBody Tag tag, @Auth User current) {
        return new ResponseEntity(userTagService.addTag(tag, current), HttpStatus.CREATED);
    }

    @DeleteMapping("tags/{tagId}")
    public ResponseEntity deleteTag(@PathVariable Long tagId) throws BusinessException {
        userTagService.deleteTag(tagId);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @PutMapping("{programId}/students/{studentId}/tags/{tagIds}")
    public ResponseEntity editStudentTags(@PathVariable Long programId,
                                          @PathVariable Long studentId,
                                          @PathVariable String tagIds,
                                          @Auth User current) {
        userTagService.editStudentTags(programId, studentId, tagIds, current);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
}
